/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.product;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hutech09.jsale.web.manager.model.LazyProductDataModel;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.entity.ProductImage;
import org.hutech09.jsale.web.manager.AppConstrant;
import org.hutech09.jsale.web.manager.price.PriceBrowseCtrl;
import org.hutech09.jsale.web.manager.product.info.PropertyCtrl;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class ProductBrowseCtrl implements Serializable {
    
    @EJB
    private IProductManager productManager;
    
    private LazyProductDataModel dataModel;
    
    private Product selected;
    
    private ProductImage selectedImage;
    
    @ManagedProperty("#{priceBrowseCtrl}")
    private PriceBrowseCtrl priceBrowse;
    
    @ManagedProperty("#{propertyCtrl}")
    private PropertyCtrl propertyCtrl;
    
    @ManagedProperty("#{categoryList}")
    private CategoryList categoryList;
    
    private List<ProductImage> productImages = new ArrayList<ProductImage>();
    
    
    @PostConstruct
    public void init() {
        dataModel = new LazyProductDataModel(productManager);
    }
    
    public ProductBrowseCtrl() {
    }
    
    public Product getSelected() {
        if (selected == null) {
            return new Product();
        }
        return selected;
    }
    
    public void setSelected(Product selected) {
        this.selected = selected;
        try {
            productImages = productManager.getProductImages(selected.getId());
            propertyCtrl.showProperties(selected);
        } catch (ItemNotFoundException ex) {
            Logger.getLogger(ProductBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
            productImages = new ArrayList<ProductImage>();
        }
        categoryList.setSelectedProduct(selected);
    }
    
    public LazyProductDataModel getDataModel() {
        return dataModel;
    }
    
    public void setDataModel(LazyProductDataModel dataModel) {
        this.dataModel = dataModel;
    }
    
    public void onRowSelect(SelectEvent event) {
    }
    
    public List<ProductImage> getProductImages() {
        if (productImages == null || productImages.isEmpty()) {
            List<ProductImage> images = new ArrayList<ProductImage>();
            ProductImage image = new ProductImage((long) -1);
            image.setImagePath("empty.png");
            image.setProduct(new Product((long) -1));
            images.add(image);
            return images;
        }
        return productImages;
    }
    
    public String updateProduct() {
        try {
            productManager.update(selected);
        } catch (ItemNotFoundException ex) {
            Logger.getLogger(ProductCreateCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "productPage";
    }
    
    public ProductImage getSelectedImage() {
        return selectedImage;
    }
    
    public void setSelectedImage(ProductImage selectedImage) {
        this.selectedImage = selectedImage;
    }
    
    public void deleteImage() {
        try {
            productManager.deleteImage(selectedImage.getId());
            productImages = productManager.getProductImages(selected.getId());
            File f = new File(AppConstrant.IMAGE_DIRECTORY + selectedImage.getImagePath());
            f.deleteOnExit();
        } catch (ItemNotFoundException ex) {
//            Logger.getLogger(ProductBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String gotoPriceBrowsePage() {
        System.err.println("goto priceBrowsePage");
        priceBrowse.setProduct(selected);
        return "priceBrowsePage";
    }
    
    public void delete() {
        System.err.println("delete : " + selected);
    }
    
    public PriceBrowseCtrl getPriceBrowse() {
        return priceBrowse;
    }
    
    public void setPriceBrowse(PriceBrowseCtrl priceBrowse) {
        this.priceBrowse = priceBrowse;
    }

    public void setPropertyCtrl(PropertyCtrl propertyCtrl) {
        this.propertyCtrl = propertyCtrl;
    }

    public PropertyCtrl getPropertyCtrl() {
        return propertyCtrl;
    }

    public CategoryList getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(CategoryList categoryList) {
        this.categoryList = categoryList;
    }
    
}
